package vip.ifmm.lehouse.email.service.util;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;

/**
 * 邮件工具类
 *
 * @author Fish
 * <p>Email: fishinlove@163.com</p>
 * <p>created by 2019/06/17 22:33:02</p>
 */
public class EmailUtil {

    /**
     * 创建文本邮件
     *
     * @param sender    发件人
     * @param topic     邮件主题
     * @param content   邮件内容
     * @param receivers 收件人们
     */
    public static SimpleMailMessage createTextEmail(String sender, String topic, String content, String... receivers) {
        SimpleMailMessage result = new SimpleMailMessage();
        result.setFrom(sender);
        result.setTo(receivers);
        result.setSubject(topic);
        result.setText(content);
        result.setSentDate(new Date());
        return result;
    }

    /**
     * 创建 HTML 邮件
     *
     * @param receivers 收件人们
     * @param topic     邮件主题
     * @param content   邮件内容
     */
    public static void mimeMessageWrapper(String sender, String topic, String content,
                                                 MimeMessage message, String... receivers) throws MessagingException {
        // 如果需要增加附件，就用这个 helper 增加
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(sender);
        helper.setTo(receivers);
        helper.setSubject(topic);
        helper.setText(content, true);
    }
}
